package com.kangyi.admin.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kangyi.admin.mapper.AdminUserMapper;
import com.kangyi.admin.mapper.EmployeeMapper;
import com.kangyi.admin.service.EmployeeService;
import com.kangyi.constant.MessageConstant;
import com.kangyi.constant.PasswordConstant;
import com.kangyi.context.BaseContext;
import com.kangyi.dto.AddEmployeeDTO;
import com.kangyi.dto.EditEmployeeDTO;
import com.kangyi.dto.EmployeePageQueryDTO;
import com.kangyi.entity.AdminUser;
import com.kangyi.exception.PermissionsException;
import com.kangyi.result.PageResult;
import com.kangyi.vo.AdminUserVO;
import com.kangyi.vo.EmployeeDetailVO;
import com.kangyi.vo.EmployeePageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;

    // 调用后检查当前线程id是否有足够的权限进行后续操作
    public void validateUserPermissions() {
        Integer currentId = BaseContext.getCurrentId();
        AdminUserVO adminUserVO = adminUserMapper.selectById(currentId);
        // 判断权限是否既不是1也不是2，如果是则抛出异常
        if (!adminUserVO.getPermissions().equals(1) && !adminUserVO.getPermissions().equals(2)) {
            throw new PermissionsException(MessageConstant.INSUFFICIENT_PRIVILEGE);
        }
    }

    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        validateUserPermissions();
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
        Page<EmployeePageQueryVO> page = employeeMapper.pageQuery(employeePageQueryDTO);
        long total = page.getTotal();
        List<EmployeePageQueryVO> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void deleteEmployee(List<Integer> ids) {
        validateUserPermissions();
        employeeMapper.deleteEmployee(ids);
    }

    @Override
    public void addEmployee(AddEmployeeDTO addEmployeeDTO) {
        validateUserPermissions();
        AdminUser adminUser = new AdminUser();
        BeanUtil.copyProperties(addEmployeeDTO,adminUser);
        adminUser.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        adminUser.setStatus(AdminUser.ENABLED);
        employeeMapper.addEmployee(adminUser);
    }

    @Override
    public void editEmployee(EditEmployeeDTO editEmployeeDTO) {
        validateUserPermissions();
        AdminUser adminUser = new AdminUser();
        BeanUtil.copyProperties(editEmployeeDTO,adminUser);
        employeeMapper.editEmployee(adminUser);
    }

    @Override
    public EmployeeDetailVO detailEmployee(Integer employeeId) {
        validateUserPermissions();
        EmployeeDetailVO employeeDetailVO = employeeMapper.detailEmployee(employeeId);
        if (employeeDetailVO == null) {
            throw new PermissionsException(MessageConstant.DATA_NOT_EXIST);
        }
        return employeeDetailVO;
    }
}
